package it.valeandre.omino.fragment;

import java.util.ArrayList;
import java.util.Locale;

import it.valeandre.omino.R;
import it.valeandre.omino.adapter.FeedsAdapter;
import it.valeandre.omino.model.Item;
import it.valeandre.omino.model.RSS;
import it.valeandre.omino.net.NetworkService;
import it.valeandre.omino.net.NetworkServiceListener;
import it.valeandre.omino.utils.Utils;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class FeedsFragment extends UpgradableBaseFragment {

	public static final String TAG = "FeedsFragment";
	private View mContentView;
	private RSS mFeeds = new RSS();
	private String mUrl;
	private FeedsAdapter mAdapter;

	public FeedsFragment(String url) {
		mUrl = url;
	}

	private ArrayList<Item> convertItemDates(ArrayList<Item> items) {
		ArrayList<Item> ret = new ArrayList<Item>();
		for (Item item : items) {
			String dateString = Utils.convertLocale(item.getPubDate(), Locale.UK, Locale.ITALIAN, "EEE, dd MMM yyyy hh:mm:ss z",
					"EEE, dd MMM yyyy");
			item.setPubDate(dateString);
		}
		ret.addAll(items);
		return ret;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mContentView = inflater.inflate(R.layout.feeds_fragment, container, false);

		ListView feedList = (ListView) mContentView.findViewById(R.id.feeds_fragment_list);
//		ArrayList<Item> feeds = null;
//		try {
//			feeds = convertItemDates(mFeeds.channel.items);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		if (feeds != null && feeds.size() > 0) {
//			// mAdapter = new FeedsAdapter(mFeeds.getChannel().getItems(),
//			// getActivity());
//			mAdapter = new FeedsAdapter(feeds, getActivity());
//		} else {
//			mAdapter = new FeedsAdapter(mFeeds.getChannel().getItems(), getActivity());
//		}
		mAdapter = new FeedsAdapter(mFeeds.channel.items, getActivity());
		feedList.setAdapter(mAdapter);
		feedList.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
				getInterFragmentInterface().switchToFeedDetailFragment(mAdapter.getItem(pos));
			}
		});

		if (mFeeds != null && mFeeds.channel.items.size() > 0) {
			ArrayList<Item> feeds;
			try {
				feeds = convertItemDates(mFeeds.channel.items);
			} catch (Exception e) {
				feeds = mFeeds.channel.items;
			}
			mAdapter.setData(feeds);
			mAdapter.notifyDataSetChanged();
		} else {
			getTeamFeed(mUrl);
		}

		return mContentView;
	}

	private void getTeamFeed(String url) {
		getInterFragmentInterface().showProgress();
		NetworkService.getInstance().getTeamFeed(url, new NetworkServiceListener() {

			@Override
			public void getTeamFeedPerformed(String url, final RSS feed) {
				getInterFragmentInterface().updateTeamFeeds(url, feed);
				mFeeds = feed;
				ArrayList<Item> feeds;
				try {
					feeds = convertItemDates(mFeeds.channel.items);
				} catch (Exception e) {
					feeds = mFeeds.channel.items;
				}
				mFeeds.getChannel().setItems(feeds);
				
				mHandler.post(new Runnable() {

					public void run() {
						
						mAdapter.setData(mFeeds.getChannel().getItems());
						mAdapter.notifyDataSetChanged();

					}
				});
				getInterFragmentInterface().hideProgress();
			}

			@Override
			public void getTeamFeedFailed(int statusCode) {
				mHandler.post(new Runnable() {

					public void run() {
						getInterFragmentInterface().hideProgress();
						getInterFragmentInterface().showDialog(getActivity().getResources().getString(R.string.no_feed_available),
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface dialog, int which) {
										getInterFragmentInterface().performBack();

									}
								});
					}
				});

			}

			@Override
			public void getTeamFeedFailed(Exception e) {
				mHandler.post(new Runnable() {

					public void run() {
						getInterFragmentInterface().hideProgress();
						getInterFragmentInterface().showDialog(getActivity().getResources().getString(R.string.no_feed_available),
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface dialog, int which) {
										getInterFragmentInterface().performBack();

									}
								});

					}
				});
			}

		});
	}

	@Override
	public void refresh() {

	}

	@Override
	public void onDetach() {
		super.onDetach();
		getInterFragmentInterface().getRefreshImageButton().setEnabled(true);
		mAdapter.reset();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		getInterFragmentInterface().getRefreshImageButton().setEnabled(false);
	}

}
